import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';
import 'package:lib_ui/lib_ui.dart';

class PlatformTextViewPage extends StatefulWidget {
  const PlatformTextViewPage({super.key});

  @override
  State<PlatformTextViewPage> createState() => _PlatformTextViewPageState();
}

class _PlatformTextViewPageState extends State<PlatformTextViewPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: TransparentAppBar(
        title: '原生TextView',
      ),
      // android用的是PlatformViewLink或者AndroidView；ios用的是UiKitView
      body: PlatformViewLink(
        surfaceFactory: (context, controller) {
          return AndroidViewSurface(
            controller: controller as AndroidViewController,
            hitTestBehavior: PlatformViewHitTestBehavior.opaque,
            gestureRecognizers: const <Factory<OneSequenceGestureRecognizer>>{},
          );
        },
        onCreatePlatformView: (params) {
          return PlatformViewsService.initSurfaceAndroidView(
            id: params.id,
            viewType: 'simple_view',
            layoutDirection: TextDirection.ltr,
            creationParams: {'text': 'Hello from Flutter!'},
            creationParamsCodec: const StandardMessageCodec(),
          )
            ..addOnPlatformViewCreatedListener(params.onPlatformViewCreated)
            ..create();
        },
        viewType: 'simple_view',
      ),
    );
  }
}
